import { ElMessage } from "element-plus";

let baseUrl = "";

const fetch = (url, options) => {
  const reqUrl = baseUrl + url;
  return new Promise((resolve, reject) => {
    useFetch(reqUrl, { ...options })
      .then(({ data, error }) => {
        if (error.value) {
          reject(error.value);
          return;
        }
        const value = data.value;
        if (!value) {
          // 这里处理错误回调
        } else if (value.code == 102) {
          ElMessage({
            message: value.msg,
            type: "error",
          });
        } else {
          resolve(value);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
};

export default new (class Http {
  get(url, params) {
    return fetch(url, { method: "get", params });
  }

  post(url, params) {
    return fetch(url, { method: "post", params });
  }

  put(url, body) {
    return fetch(url, { method: "put", body });
  }

  delete(url, body) {
    return fetch(url, { method: "delete", body });
  }
})();
